<template>
  <div class="app-container">
    <el-form :inline="true" :model="form" ref="form-inline">
      <el-form-item label="企业名称" prop="entName">
        <el-input v-model="form.entName" placeholder="企业名称"></el-input>
      </el-form-item>
      <el-form-item label="企业信用代码" prop="entUniId">
        <el-input
          style="width: 250px"
          v-model="form.entUniId"
          placeholder="企业信用代码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="goSearch">查询 </el-button>
        <el-upload
          ref="upload"
          accept=".xlsx"
          class="upload-demo"
          :on-success="onSuccess"
          :on-progress="onProgress"
          :on-error="handeError"
          :with-credentials="true"
          :action="uploadUrl"
          :disabled="uploadLoading ? true : false"
          multiple
        >
          <el-button class="el-upload__btn" :loading="uploadLoading" type="info"
            >{{ uploadLoading ? "上传中" : "上传"
            }}<i class="el-icon-upload el-icon--right"></i
          ></el-button>
        </el-upload>
        <el-button style="margin-left: 10px;" type="success" :disabled="!tableData.length" @click="downLoad">下载 </el-button>
      </el-form-item>
    </el-form>

    <Table 
        ref="Table" 
        :tableData="tableData" 
        :tableHeadData="tableHeadData" 
        :tableRowClassName="tableRowClassName"
        :total="total"
        v-loading="tableLoading"
        @getList="search" 
        :DefaultPageParams="currentPageParams"
    />
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        width="1400px">
        <Table 
            ref="dialogTable" 
            :tableData="dialogTableData" 
            :tableHeadData="dialogTableHeadData"
        />
      </el-dialog>  
  </div>
</template>

<script>
import Table from "@/components/table/table.vue";
import { getTableList,parseTime } from '@/utils/index'
import { JueceCommon } from "./juece.common";
import { queryHis,batchDetail } from "@/api/user";
import bus from '@/utils/bus.js'
import * as XLSX from 'xlsx/xlsx.mjs';

export default {
  components: {
    Table,
  },
  data() {
    return {
    form:{
        entName:'',
        entUniId:''
      },
      tableData:[],
      total:0,
      dialogTableData:[],
      tableHeadData:[],
      dialogTableHeadData:[],
      tableLoading:false,
      currentPageParams:{
          'pageNum': 1,
          'pageSize': 10
      },
      uploadUrl: "",
      uploadLoading: false,
      dialogVisible:false,
      dialogTitle:'',
    };
  },
  created() {
    this.jueceCommon = new JueceCommon(this);
    this.uploadUrl = process.env.VUE_APP_BASE_API + "/s/queryHisByExcel";
  },
  mounted(){
   bus.$on('es_link',(...data) => {
      this.handelLink(data[0],data[1])
    })
  },
  methods: {
    async handelLink(row,type){
      //console.log('result',row,type)
      const params = {
        type,
        batchId:row.id
        //batchId:278
      };
      this.tableLoading = true;
      const result = await batchDetail(params);
      //console.log('result',result)
      const { data } = result;
      this.dialogTableData = data;

      this.$nextTick(_ =>{
        if(type === 'bzx'){
          this.dialogTitle = '被执行数据';
          this.dialogTableHeadData = this.jueceCommon.tableHead_bzx(data[0]);
        }else if(type === 'ktgg'){
          this.dialogTitle = '开庭数据';
          this.dialogTableHeadData = this.jueceCommon.tableHead_ktgg(data[0]);
        }else if(type === 'laxx'){
          this.dialogTitle = '立案数据';
          this.dialogTableHeadData = this.jueceCommon.tableHead_la(data[0]);
        }else{
          this.dialogTitle = '限高数据';
          this.dialogTableHeadData = this.jueceCommon.tableHead_xzgxf(data[0]);
        }
      })
      this.tableLoading = false;
      this.dialogVisible = true;
      
    },
    goSearch(){
        this.DATA = null;
        this.currentPageParams = {
            pageNum:1,
            pageSize:10
        };
        this.search()
    },
    async search({ pageSize = 10, pageNum = 1 } = this.currentPageParams) {
        if(this.DATA && this.DATA.length){
            this.createTableDate(pageNum,pageSize,this.DATA)
        }else{
            const { entName,entUniId } = this.form;
            if(!entName && !entUniId){
                this.$message.error('请输入企业名称或者企业信用代码');
                return
            }
            const params = {
                entName:entName === '' ? null : entName,
                entUniId:entUniId === '' ? null : entUniId
            };
            //console.log('search',this.form)
            //this.getTableList(this.currentPageParams)
            this.tableLoading = true;
            const res = await queryHis(params);
            console.log('res',res);
            this.DATA = res.data;
            this.createTableDate(pageNum,pageSize,this.DATA)
            this.tableLoading = false;
        }
        
        
    },
    createTableDate(pageNum,pageSize,DATA){
        this.total = DATA.length;
        const currentData = getTableList(pageNum,pageSize,DATA)
        console.log('currentData',currentData);
        this.tableData = this.jueceCommon.fomatData_ES(currentData);
        this.tableHeadData = this.jueceCommon.tableHead_ES(currentData[0], this);
    },
    onSuccess(response, file, fileList) {
      //console.log('onSuccess',response)
      if (response.message === "SUCCESS") {
        this.$message.success("上传成功");
        this.tableLoading = true;
        this.DATA = response.data;
        this.createTableDate(1,10,this.DATA)
        this.tableLoading = false;
        this.uploadLoading = false;
        this.currentPageParams = {
            pageNum:1,
            pageSize:10
        };
        this.form = {
            entName: "",
            entUniId: "",
        };
      } else {
        this.$message.error(response.message);
      }
    },
    onProgress(ev) {
      //console.log('onProgress',ev)
      this.uploadLoading = true;
    },
    handeError(err, file) {
      this.uploadLoading = false;
      this.$message.error("Internal Server Error");
    },
    async downLoad() {
        //https://blog.csdn.net/weixin_45502613/article/details/109745994
        //console.log('this.tableHeadData',this.tableHeadData) 
        //console.log('this.DATA',this.DATA) 
        const header = this.tableHeadData.map(item => item.label)
        let tableData = [header];
        this.DATA.forEach((i,idx) => {
            i.createTime = i.createTime ? parseTime(new Date(i.createTime).getTime(), '{y}-{m}-{d} {h}:{i}:{s}') : null
            i.regTime = i.regTime ? parseTime(new Date(i.regTime).getTime(), '{y}-{m}-{d} {h}:{i}:{s}') : null
            i.dataTime = i.dataTime ? parseTime(new Date(i.dataTime).getTime(), '{y}-{m}-{d} {h}:{i}:{s}') : null
            i.gszcTime = i.gszcTime ? parseTime(new Date(i.gszcTime).getTime(), '{y}-{m}-{d} {h}:{i}:{s}') : null

            let rowData = [];
            this.tableHeadData.map(item => item.prop || item.prop2).forEach((item,index) => {
                const prop = item.indexOf('_view') !== -1 ? item.slice(0,item.indexOf('_view')) : item
                rowData.push(i[prop])
            })
            tableData.push(rowData)
        })
       // console.log('tableData',tableData)
        let ws = XLSX.utils.aoa_to_sheet(tableData)
        let wb = XLSX.utils.book_new()
        XLSX.utils.book_append_sheet(wb, ws, '历史信息查询') // 工作簿名称
        XLSX.writeFile(wb, '历史信息查询.xlsx') // 保存的文件名

    //   const url = process.env.VUE_APP_BASE_API + '/batch/export?batchNo=' + this.form.batchNo;
    //   window.open(url,'_blank')
    },
    tableRowClassName ({row}) { // 点击行获取table的行索引
        if (row.isOK === '通过') {
            return 'current-row';
        }
    },
  },
};
</script>

<style scoped lang="scss">
.upload-demo {
  display: inline-block;
  margin-left: 10px;
  ::v-deep .el-upload-list--text {
    display: none;
  }
}
::v-deep .pagination {
  .el-pagination{
    text-align: center;
  }
 }
 ::v-deep .el-table__header{
  .cell{
    padding: 0 5px;
  }
 }
 ::v-deep .el-table__body tr.current-row>td{
      background-color: rgba(204,255,230,.3);
  }
</style>
